// component/detailMask/detailMask.js
const
    globalData = getApp().globalData,
    utils = require('../../utils/util');
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        visible: {
            type: Boolean, //控制显示
            value: false,
        },
        title: {
            type: String,
            value: '标题'
        },
        bottom: {
            type: Number,
            value: -1
        },
        closeToHome: {
            type: Boolean,
            value: false
        },
    },

    observers: {
        'visible': function (newV, oldV) {
            newV ? this.showSheet() : this.closeSheet();
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        startPoint: void 0,
        animationData: {},
        distc: void 0,
        imageBaseUri: globalData.imageBaseUri,
    },
    created() {
        //创建一个动画实例
        let animation = wx.createAnimation({
            duration: 300,
            timingFunction: 'ease',
        });

        this.animation = animation
    },
    /**
     * 组件的方法列表
     */
    methods: {
        //打开弹层
        async showSheet() {
            await utils.sleep(50);
            this.animation.translate(0, 0).step();

            this.setData({
                animationData: this.animation.export()
            });
        },
        //关闭弹层
        closeSheet() {
            this.animation.translate(0, globalData.SystemInfo.windowHeight).step();

            this.setData({
                animationData: this.animation.export()
            })
        },
        //触摸记录坐标点
        headerStart(e) {
            this.animation.option.transition.duration = 0;

            this.setData({
                startPoint: e.touches[0]
            });
        },
        //移动改变动画时间和Y轴
        headerMove(e) {
            let endPoint = e.touches[e.touches.length - 1];

            let startPoint = this.data.startPoint.clientY;

            let distc = endPoint.clientY - startPoint;

            this.animation.option.transition.duration = 0; //改变动画时间

            this.animation.translate(0, distc).step();

            this.setData({
                animationData: this.animation.export(),
                distc: distc
            })
        },
        //松开判断滑动距离
        async headerEnd(e) {
            let distc = this.data.distc;

            if (distc <= 150) {
                this.animation.option.transition.duration = 300;

                this.showSheet();
            }
            if (distc > 150) {

                this.animation.option.transition.duration = 300;

                this.closeSheet();

                await utils.sleep(300);
                this.setData({ visible: false });
                this.goHome();
            }
        },

        /**
         * 返回首页
         */
        goHome() {
            if (this.data.closeToHome) wx.reLaunch({ url: globalData.homeUri });
        },

        async close() {
            this.closeSheet();

            await utils.sleep(300);
            this.setData({ visible: false });
            this.goHome();
            this.triggerEvent("close");
        }
    }
})
